//定时器间隔  
var interval=1000;
var objInterval=null;
var timers=120;
//去除空格正则表达式
var reg_null = /\s+/g;
//邮箱正则表达式
var reg_email = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;

$(function(){
	$("#validate").bind("click",function(){
		var to = $("#userEmail").val();
		
		if(!reg_email.test(to)){
			$("#emailMessage").html("你输入的Email无效");
			$("#emailMessage").removeClass("yes");
			$("#emailMessage").addClass("error");
			return false;
		}else{
			$("#emailMessage").html("");
			$("#emailMessage").removeClass("error");
			$("#emailMessage").addClass("yes");
		}
		//密码验证
		if($("#psw_one").val()!=$("#psw_two").val()){
			$("#message_one,#message_two").html("两次输入的密码不一致");
			$("#message_one,#message_two").removeClass("yes");
			$("#message_one,#message_two").addClass("error");
			return false;
		}else if($("#psw_one").val().length<6 || $("#psw_one").val().length>20){
			$("#message_one,#message_two").html("密码的长度必须在6——20之间");
			$("#message_one,#message_two").removeClass("yes");
			$("#message_one,#message_two").addClass("error");
			return false;
		}else{
			$("#message_one,#message_two").html("");
			$("#message_one,#message_two").removeClass("error");
			$("#message_one,#message_two").addClass("yes");
		}
		
		$('[name="check"]').val($('[name="check"]').val().replace(reg_null,""));
		if($('[name="check"]').val().length<1){
			return false;
		}
		//启动定时器
		StartTimer();
		$.ajax({
		   type: "POST",
		   url: "/forward/email/retrieve_validate.go",
		   data: "to="+to,
		   success: function(msg){
			//成功返回后的操作
		   }
		});
	});
	//电子邮箱
	$("#userEmail").addClass("fontColor");
	$("#userEmail").val("请输入您的Email邮箱");
	
	$("#userEmail").bind("focus",function(){
		if($("#userEmail").val()=="请输入您的Email邮箱"){
			$("#userEmail").val("");
			$("#userEmail").removeClass("fontColor");
		}
	});
	$("#userEmail").bind("blur",function(){
		//去空
		$("#userEmail").val($("#userEmail").val().replace(reg_null,""));
		if($("#userEmail").val()<1){
			$("#userEmail").val("请输入您的Email邮箱");
			$("#userEmail").addClass("fontColor");
		}
	});
});

//启动定时器，开始滚动
function StartTimer(){
	objInterval=setInterval("changeTimer()",interval);
}

//清除定时器，停止滚动
function StopTimer(){
	window.clearInterval(objInterval);
}

function changeTimer(){
	if(timers>0){
		timers=timers-1;
		 $("#validate").html(timers);
	}else{
		 $("#validate").html("获取验证码");
		 StopTimer();
	}
}